package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.lc.afbx.R;
import com.lc.afbx.adapters.HeZuoAdapter;
import com.lc.afbx.conn.GetApiShowCompany;
import com.lc.afbx.util.MyToast;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshGridView;
import com.zcx.helper.http.AsyCallBack;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/5/19.
 */
public class CooperationCompanyActivity extends BaseActivity implements View.OnClickListener {


    private PullToRefreshGridView pullToRefreshGridView;
    private GridView cooperation_company_gv;

    private HeZuoAdapter adapter;
    private List<GetApiShowCompany.Data> data = new ArrayList();;

    private GetApiShowCompany getApiShowCompany = new GetApiShowCompany(new AsyCallBack<GetApiShowCompany.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiShowCompany.Info info) throws Exception {

            data.addAll(info.data);

            adapter.notifyDataSetChanged();

            setLastUpdateTime();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, CooperationCompanyActivity.this);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshGridView.onPullUpRefreshComplete();
            pullToRefreshGridView.onPullDownRefreshComplete();

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cooperation_company);

        pullToRefreshGridView = ((PullToRefreshGridView) findViewById(R.id.cooperation_company_gv));
        pullToRefreshGridView.setScrollLoadEnabled(false);
        pullToRefreshGridView.setPullLoadEnabled(false);
        pullToRefreshGridView.setPullRefreshEnabled(true);

        pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {

                data.clear();
                getApiShowCompany.execute(CooperationCompanyActivity.this);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {

            }
        });

        cooperation_company_gv = pullToRefreshGridView.getRefreshableView();

        cooperation_company_gv.setNumColumns(2);
        
        cooperation_company_gv.setGravity(Gravity.CENTER);

        cooperation_company_gv.setAdapter(adapter = new HeZuoAdapter(context, data));

        cooperation_company_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(CooperationCompanyActivity.this, AnotherCompanyActivity.class));

            }
        });

        getApiShowCompany.execute(CooperationCompanyActivity.this);

    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:

                finish();

                return;

        }
    }

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshGridView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        SimpleDateFormat mDateFormat = new SimpleDateFormat("HH:mm:ss");
        return mDateFormat.format(new Date(time));
    }

}
